package com.xyc.manager.merchantxy.service.impl;


import com.xyc.manager.merchantxy.bean.WxbCustomer;
import com.xyc.manager.merchantxy.mapper.MerchantXYMapper;
import com.xyc.manager.merchantxy.service.MerchantService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.Random;

@Service
public class MerchantServiceImp implements MerchantService {
    @Resource
    private MerchantXYMapper merchantXYMapper;
    @Override
    public List<WxbCustomer> queryMerchantList() throws Exception {
        List<WxbCustomer> customers = merchantXYMapper.queryMerchantList();
        return customers;
    }

    @Override
    public void addMerchant(WxbCustomer wxbCustomer) throws Exception {

        Random random = new Random();
        int nextInt = random.nextInt(1000000);
        Md5Hash md5Hash = new Md5Hash(wxbCustomer.getLoginPwd());
        String hex = md5Hash.toHex();
        wxbCustomer.setLoginPwd(hex);

        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        wxbCustomer.setCreatetime(timestamp);
        wxbCustomer.setCustomerId("xy"+nextInt);

        merchantXYMapper.addMerchant(wxbCustomer);
    }
}
